package com.pactera.madp.cp.controller.oms.sensitive;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.entity.sensitive.SensitiveWordsMonitorEntity;
import com.pactera.madp.cp.api.dto.sensitive.ReqSensitiveWordsMonitorDto;
import com.pactera.madp.cp.api.vo.sensitive.SensitiveWordMonitorDetailsVO;
import com.pactera.madp.cp.api.vo.sensitive.SensitiveWordMonitorPageVO;
import com.pactera.madp.cp.service.ISensitiveWordsMonitorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @description:敏感词监控
 * @author: Huayu
 * @time: 2021/2/4 9:40
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sensitivewordsmonitor")
@Api(value = "sensitivewordsmonitor", tags = "敏感词监控")
public class SensitiveWordsMonitorController {

    private ISensitiveWordsMonitorService sensitiveWordsMonitorImpl;

    @GetMapping("/index")
    @ApiOperation(value = "敏感词监控列表")
    public R<Page<SensitiveWordMonitorPageVO>> sensitiveWordsMonitorIndex(ReqSensitiveWordsMonitorDto reqSensitiveWordsMonitorDto, Page page){
        return R.ok(sensitiveWordsMonitorImpl.handle(reqSensitiveWordsMonitorDto,page));
    }

    @GetMapping("/{sensitiveWordsMonitorId}")
    @ApiOperation(value = "敏感词监控详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name="sensitiveWordsMonitorId",value="敏感词监控id",dataType ="String")
    })
    public R sensitiveWordsMonitorShow(@PathVariable String sensitiveWordsMonitorId){
        List<SensitiveWordMonitorDetailsVO> vos = new LinkedList<>();
        if(StringUtil.isEmpty(sensitiveWordsMonitorId)){
            return R.ok("敏感词监控ID不能为空");
        }
        //获取敏感词监控信息
        SensitiveWordsMonitorEntity sensitiveWordsMonitorEntity = sensitiveWordsMonitorImpl.getSensitiveWordMonitorById(sensitiveWordsMonitorId);
        if(sensitiveWordsMonitorEntity == null ||sensitiveWordsMonitorEntity.getChatContent() == null ){
            return  R.ok("当前敏感词监控信息不存在");
        }
        String content = sensitiveWordsMonitorEntity.getChatContent();
        if(content != null && !content.equals("")){
            JSONArray jsonArray = JSON.parseArray(content);
            if(jsonArray != null && jsonArray.size() > 0){
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                    SensitiveWordMonitorDetailsVO vo = sensitiveWordsMonitorImpl.contentFormat(jsonObject1);
                    vo.setSource(sensitiveWordsMonitorEntity.getSource());
                    vos.add(vo);
                }
            }
        }
        return R.ok(vos);
    }

}
